package uit.nfcmenu.webservice;

import java.util.Locale;

import uit.nfcmenu.R;
import uit.nfcmenu.util.CommConstant;
import android.content.Context;
import android.content.SharedPreferences;



public class PreferenceHelper {
	private static final String DEFAULT_LANGUAGE = Locale.ENGLISH.getLanguage();
	
	// FIXME hard code 4 test >
	public static final String DEFAULT_USER = "user";
	public static final String DEFAULT_RESTAURANT_NAME = "Default RestName";
	public static final String DEFAULT_RESTAURANT_ID = "2";
	public static final String DEFAULT_DEVICE_NAME = "1";
	public static final String DEFAULT_USER_TOKEN = "";
	// />
	
	public static final String DEFAULT_DEVICE_ID = "";
//	public static final String DEFAULT_USER = "";
//	public static final String DEFAULT_RESTAURANT_NAME = "";
//	public static final String DEFAULT_RESTAURANT_ID = "";
//	public static final String DEFAULT_DEVICE_NAME = "";
//	public static final String DEFAULT_USER_TOKEN = "";
	public static final int DEFAULT_VERSION_MENU = -1;
	public static final int DEFAULT_VERSION_ITEM = -1;
	public static final int DEFAULT_VERSION_ORDER = -1;

	public static final String PREF_LANGUAGE = "pref_Language";

	public static final String PREF_USER = "pref_User";
	public static final String PREF_RESTAURANT_NAME = "pref_RestName";
	public static final String PREF_RESTAURANT_ID = "pref_RestId";
	public static final String PREF_DEVICE_ID = "pref_DeviceId";
	public static final String PREF_DEVICE_NAME = "pref_DeviceName";
	public static final String PREF_USER_TOKEN = "pref_UserToken";
	public static final String PREF_VERSION_MENU = "pref_VersionMenu";
	public static final String PREF_VERSION_ITEM = "pref_VersionItem";
	public static final String PREF_VERSION_ORDER = "pref_VersionOrder";

	public static final String mPreferenceName = R.class.toString();

	// ==================================
	// Util methods
	// ==================================
	/*
	 * get, set Language
	 */
	public static void saveLanguage(Context context, String language) {
		savePreferences(context, PREF_LANGUAGE, language);
	}
	
	public static String loadLanguage(Context context) {
		return loadPreferences(context, PREF_LANGUAGE, DEFAULT_LANGUAGE);
	}
	
	/*
	 * get, set Username
	 */
	public static void saveUsername(Context context, String username) {
		savePreferences(context, PREF_USER, username);
	}
	
	public static String loadUsername(Context context) {
		return loadPreferences(context, PREF_USER, DEFAULT_USER);
	}
	
	/*
	 * get, set Restaurant
	 */
	public static void saveRestaurantName(Context context, String restName) {
		savePreferences(context, PREF_RESTAURANT_NAME, restName);
	}
	
	public static String loadRestaurantName(Context context) {
		return loadPreferences(context, PREF_RESTAURANT_NAME, DEFAULT_RESTAURANT_NAME);
	}
	
	/*
	 * get, set RestaurantId
	 */
	public static void saveRestaurantId(Context context, String restId) {
		savePreferences(context, PREF_RESTAURANT_ID, restId);
	}
	
	public static String loadRestaurantId(Context context) {
		return loadPreferences(context, PREF_RESTAURANT_ID, DEFAULT_RESTAURANT_ID);
	}
	
	/*
	 * get, set DeviceId
	 */
	public static void saveDeviceId(Context context, String deviceId) {
		savePreferences(context, PREF_DEVICE_ID, deviceId);
	}
	
	public static String loadDeviceId(Context context) {
		return loadPreferences(context, PREF_DEVICE_ID, DEFAULT_DEVICE_ID);
	}
	
	public static void saveDeviceName(Context context, String deviceName) {
		savePreferences(context, PREF_DEVICE_NAME, deviceName);
	}
	
	public static String loadDeviceName(Context context) {
		return loadPreferences(context, PREF_DEVICE_NAME, DEFAULT_DEVICE_NAME);
	}
	
	/*
	 * get, set UserToken
	 */

	public static void saveUserToken(Context context, String userToken) {
		savePreferences(context, PREF_USER_TOKEN, userToken);
	}

	public static String loadUserToken(Context context) {
		return loadPreferences(context, PREF_USER_TOKEN, DEFAULT_USER_TOKEN);
	}
	
	/*
	 * get, set Versions
	 */
	public static void saveVersion(Context context, int type, int version) {
		switch (type) {
		case CommConstant.CHANGED_MENU:
			savePreferences(context, PREF_VERSION_MENU, version);
			break;
		case CommConstant.CHANGED_ITEM:
			savePreferences(context, PREF_VERSION_ITEM, version);
			break;
		case CommConstant.CHANGED_ORDER:
			savePreferences(context, PREF_VERSION_ORDER, version);
			break;

		default:
			break;
		}
	}
	
	public static int loadVersion(Context context, int type) {
		switch (type) {
		case CommConstant.CHANGED_MENU:
			return loadPreferences(context, PREF_VERSION_MENU, DEFAULT_VERSION_MENU);
		case CommConstant.CHANGED_ITEM:
			return loadPreferences(context, PREF_VERSION_ITEM, DEFAULT_VERSION_ITEM);
		case CommConstant.CHANGED_ORDER:
			return loadPreferences(context, PREF_VERSION_ORDER, DEFAULT_VERSION_ORDER);
		default:
			return 0;
		}
	}
	
	// ==================================
	// Common methods
	// ==================================
	public static void savePreferences(Context context, String key, String value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				mPreferenceName, Context.MODE_WORLD_READABLE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	public static void savePreferences(Context context, String key, int value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				mPreferenceName, Context.MODE_WORLD_READABLE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	public static void savePreferences(Context context, String key, boolean value) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				mPreferenceName, Context.MODE_WORLD_READABLE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	public static String loadPreferences(Context context, String key, String backup) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				mPreferenceName, Context.MODE_WORLD_READABLE);
		String result = sharedPreferences.getString(key, backup);
		return result;
	}
	
	public static int loadPreferences(Context context, String key, int backup) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				mPreferenceName, Context.MODE_WORLD_READABLE);
		int result = sharedPreferences.getInt(key, backup);
		return result;
	}

	public static boolean loadPreferences(Context context, String key, boolean backup) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				mPreferenceName, Context.MODE_WORLD_READABLE);
		boolean result = sharedPreferences.getBoolean(key, backup);
		return result;
	}
}
